package com.shanling.shanlingcontroller.view;

import android.app.Activity;
import android.content.Context;
import android.graphics.drawable.ColorDrawable;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.PopupWindow;
import android.widget.TextView;

import com.shanling.shanlingcontroller.CustomApplication;
import com.shanling.shanlingcontroller.R;
import com.shanling.shanlingcontroller.adapter.CommonAdapter;
import com.shanling.shanlingcontroller.adapter.ViewHolder;
import com.shanling.shanlingcontroller.base.BaseAppManager;
import com.shanling.shanlingcontroller.bean.EQBean;
import com.shanling.shanlingcontroller.bean.Music;
import com.shanling.shanlingcontroller.music.player.PlayManager;
import com.shanling.shanlingcontroller.music.player.playqueue.PlayQueueManager;
import com.shanling.shanlingcontroller.ui.activity.MusicPlayActivity;
import com.shanling.shanlingcontroller.utils.DpUtils;
import com.shanling.shanlingcontroller.utils.ToastUtils;

import java.util.ArrayList;
import java.util.List;


/**
 * Created by ch1 on 2019/12/7.
 */

public class EQPopupWindow {
    private CommonAdapter<EQBean> eqCommonAdapter;
    private PopupWindow popupWindow;
    private View popupView;
    private Activity activity;
    private ItemOnClickInterface itemOnClickInterface;

    public EQPopupWindow(Activity activity) {
        this.activity=activity;
        initPopwwindow(activity);
    }

    private void initPopwwindow(Context mContext) {
        popupView = LayoutInflater.from(mContext).inflate(R.layout.layout_eqpop, null);
        popupWindow = new PopupWindow(popupView, ViewGroup.LayoutParams.MATCH_PARENT, DpUtils.dpToPx( mContext,400), true);
        popupWindow.setTouchable(true);
        popupWindow.setOutsideTouchable(true);
        popupWindow.setFocusable(true);
        popupWindow.setAnimationStyle(R.style.PopupAnimation);
        popupWindow.setBackgroundDrawable(new ColorDrawable(mContext.getResources().getColor(R.color.bg_normal)));
        popupWindow.getContentView().setFocusableInTouchMode(true);
        popupWindow.getContentView().setFocusable(true);
        List<EQBean> eqBeans=new ArrayList<>();
        eqCommonAdapter=new CommonAdapter<EQBean>(mContext,eqBeans ,R.layout.item_eq) {
            @Override
            public void convert(ViewHolder helper, int position, EQBean item) {
                TextView tv_eq=helper.getView(R.id.tv_eq);
                ImageView iv_eq=helper.getView(R.id.iv_eq);
                tv_eq.setText(item.getEqName());
                if(item.isCheck()){
                    iv_eq.setVisibility(View.VISIBLE);
                }else {
                    iv_eq.setVisibility(View.GONE);
                }
            }
        };
        popupWindow.setOnDismissListener(new PopupWindow.OnDismissListener() {
            @Override
            public void onDismiss() {
                bgAlpha(1.0f,activity);
            }
        });
        popupView.findViewById(R.id.tv_cancle).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                popupWindow.dismiss();
            }
        });
    }
    public void show(List<EQBean> eqBeans, View view){
        if(popupView!=null&&view!=null){
            ListView iv_cover=popupView.findViewById(R.id.lv_eq);
            eqCommonAdapter.refresh(eqBeans);
            iv_cover.setAdapter(eqCommonAdapter);
            iv_cover.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                @Override
                public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                    for (int i = 0; i < eqBeans.size(); i++) {
                        eqBeans.get(i).setCheck(false);
                    }
                    eqBeans.get(position).setCheck(true);
                    eqCommonAdapter.refresh(eqBeans);
                    if(itemOnClickInterface!=null){
                        itemOnClickInterface.onItemClick(view,position);
                    }
                }
            });
            popupWindow.showAtLocation(view, Gravity.BOTTOM,0,0);
            bgAlpha(0.4f,activity);
        }
    }
    public void refrash(List<EQBean> eqBeans){
        eqCommonAdapter.refresh(eqBeans);
    }


    public void dissmiss(){
        if(popupWindow.isShowing()){
            popupWindow.dismiss();
        }
        popupWindow=null;
    }

    private  void bgAlpha(float f,Activity activity){
        WindowManager.LayoutParams layoutParams = activity.getWindow().getAttributes();
        layoutParams.alpha = f;
        activity.getWindow().setAttributes(layoutParams);
    }

    public ItemOnClickInterface getItemOnClickInterface() {
        return itemOnClickInterface;
    }

    public void setItemOnClickInterface(ItemOnClickInterface itemOnClickInterface) {
        this.itemOnClickInterface = itemOnClickInterface;
    }

    //回调接口
    public interface ItemOnClickInterface {
        void onItemClick(View view, int position);
    }

}
